<?php
namespace Plu\Library\Models\Platform\Recruit;

use Phalcon\Mvc\Model,
    Phalcon\Db\Column;

class ApplyRoomCommentary extends Model {
    public $Id;

    public $UserId;

    public $TrueName;

    public $QQ;

    public $Phone;

    public $ApplyGameId;

    public $MyDesc;

    public $MyProduct;

    public $MyPic1;

    public $MyPic2;

    public $MyPic3;

    public $Status;

    public $ApplyTime;

    public $ApproveTime;

    public $ApproveNote;

    public $BanUserId;

    public $ClientIP;

    const StatusType = array(
        'INIT'		=> 0,
        'ACCEPTED'  => 1,
        'REJECTED'	=> 2
    );

    public function initialize() {
        $this->setConnectionService('db.platform');
        $this->allowEmptyStringValues(array('ApproveTime', 'ApproveNote', "BanUserId"));
    }

    public function getSource() {
        return "ApplyRoomCommentary";
    }

    /**
     * @method 通过用户id获取直播申请
     * @param int $userid
     * @return mixed
     */
    public static function getCommentaryByUid($userid){
        $item = self::findFirst(array(
            "UserId = :uid:",
            "bind"	=> array(
                'uid' => $userid
            ),
            "bindTypes" => array(
                'uid' => Column::BIND_PARAM_INT
            ),
            'order'	=> "Id DESC"
        ));

        return $item;
    }

    /**
     * @method 通过id获取直播申请
     * @param int $id
     * @return mixed
     */
    public static function getCommentaryById($id) {
        $item = self::findFirst(array(
            "Id => :id:",
            "bind"	=> array(
                'id' => $id
            ),
            "bindTypes" => array(
                'id' => Column::BIND_PARAM_INT
            ),
            'order'	=> "Id DESC"
        ));

        return $item;
    }

    /**
     * 将原先的请求置为2
     */
    public static function discardRegistersByUid($uid) {
        $items = self::find(array(
            "UserId = :uid: AND ( Status = 0 OR Status = 1 )",
            "bind" => array(
                "uid" => $uid
            ),
            "bindTypes" => array(
                "uid" => Column::BIND_PARAM_INT
            )
        ));

        if (count($items)) {
            for ($c = 0, $len = count($items); $c < $len; $c++) {
                $item = $items[$c];
                $item->Status = self::StatusType['REJECTED'];
                $item->ApproveTime = date("Y-m-d H:i:s",time());
                $item->ApproveNote = 'user re-apply';
                $item->update();
            }
        }
    }
}

?>
